package com.fr.demo.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public org.springframework.cache.CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new org.springframework.cache.caffeine.CaffeineCacheManager("myCache");
        cacheManager.setCaffeine(caffeineCache());
        return cacheManager;
    }

    @Bean
    public Caffeine<Object, Object> caffeineCache() {
        return Caffeine.newBuilder()
                .maximumSize(100) // 设置最大缓存项数
                .expireAfterWrite(10, TimeUnit.SECONDS); // 设置写入后过期时间
    }
}
